<?php
class ModelMenuReview extends Model {		
	public function addReview($food_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "food_review SET author = '" . $this->db->escape($data['name']) . "', customer_id = '" . (int)$this->customer->getId() . "', food_id = '" . (int)$food_id . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', rating = '" . (int)$data['rating'] . "', date_added = NOW()");
	}
		
	public function getReviewsByFoodId($food_id, $start = 0, $limit = 20) {
		$query = $this->db->query("SELECT fr.review_id, fr.author, fr.rating, fr.text, f.food_id, fd.name, f.price, f.image, fr.date_added FROM " . DB_PREFIX . "food_review fr LEFT JOIN " . DB_PREFIX . "food f ON (fr.food_id = f.food_id) LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id) WHERE f.food_id = '" . (int)$food_id . "' AND f.date_available <= NOW() AND f.status = '1' AND fr.status = '1' AND fd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY fr.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
		
		return $query->rows;
	}
	
	public function getAverageRating($food_id) {
		$query = $this->db->query("SELECT AVG(rating) AS total FROM " . DB_PREFIX . "food_review WHERE status = '1' AND food_id = '" . (int)$food_id . "' GROUP BY food_id");
		
		if (isset($query->row['total'])) {
			return (int)$query->row['total'];
		} else {
			return 0;
		}
	}	
	
	public function getTotalReviews() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "food_review fr LEFT JOIN " . DB_PREFIX . "food f ON (fr.food_id = f.food_id) WHERE f.date_available <= NOW() AND f.status = '1' AND fr.status = '1'");
		
		return $query->row['total'];
	}

	public function getTotalReviewsByFoodId($food_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "food_review fr LEFT JOIN " . DB_PREFIX . "food f ON (fr.food_id = f.food_id) LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id) WHERE f.food_id = '" . (int)$food_id . "' AND f.date_available <= NOW() AND f.status = '1' AND fr.status = '1' AND fd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		return $query->row['total'];
	}
}
?>